package com.tr.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.tr.shopping.product.entity.AttrEntity;
import com.tr.shopping.product.service.AttrAttrgroupRelationService;
import com.tr.shopping.product.service.AttrService;
import com.tr.shopping.product.service.CategoryService;
import com.tr.shopping.product.vo.AttrGroupVo;
import com.tr.shopping.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tr.shopping.product.entity.AttrGroupEntity;
import com.tr.shopping.product.service.AttrGroupService;
import com.tr.common.utils.PageUtils;
import com.tr.common.utils.R;



/**
 * 属性分组
 *
 * @author tanrui
 * @email 2484672468@qq.com
 * @date 2021-06-19 21:20:16
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    //attrgroup/225/withattr 设置销售属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupCatelogIdWithattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrsVo = attrGroupService.getAttrgroupCatelogIdWithattrBycatelogId(catelogId);
        return R.ok().put("data",attrsVo);
    }

    //添加关联关系
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupVo[] attrGroupVos){
        attrService.deleteRelation(attrGroupVos);
        return R.ok();
    }
    //获取没有关联的属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page=  attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
     List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page= attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path=categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
